const service = require('./moneyDetailService.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    startPage: 0,
    hasNextPage: true,
    moneyType: -1, // 0 零钱明细 1 奖励明细
    items: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    if (options.title) {
      wx.setNavigationBarTitle({
        title: options.title
      });
    }
    if (options.moneyType) {
      this.setData({
        moneyType: options.moneyType
      });
      if (options.moneyType == 1) {
        this.loadPage();
      } else {
        this.loadAmountPage();
      }
    }

  },

  /**
   * 请求零钱明细分页数据
   */
  loadAmountPage: function() {
    let that = this;
    if (that.data.hasNextPage) {
      service.getUserAmountDetail(this.data.startPage, function(res) {
        if (res.items && res.items.length > 0) {
          that.setData({
            startPage: that.data.startPage,
            items: that.data.items.concat(res.items)
          });
        } else {
          that.setData({
            hasNextPage : false
          })
        }
      });
    }
  },

  /**
   * 请求分页数据
   */
  loadPage: function() {
    let that = this;
    if (that.data.hasNextPage) {
      that.setData({
        startPage: that.data.startPage + 1
      });
      service.getMoneyDetail(that.data.startPage, function(res) {
        if (res.execDatas && res.execDatas.length > 0) {
          that.setData({
            items: that.data.items.concat(res.execDatas)
          });
        }
        if (res.execDatas.length < 20) {
          that.setData({
            hasNextPage: false
          });
        }
      });
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.moneyType == 1) {
      this.loadPage();
    } else {
      this.loadAmountPage();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },
})